package cn.stylefeng.guns.modular.rw.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.UploadFileUtil;
import cn.stylefeng.guns.modular.rw.entity.Videos;
import cn.stylefeng.guns.modular.rw.mapper.VideosMapper;
import cn.stylefeng.guns.modular.rw.service.IVideosService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：视频管理表 service业务层
 * 作者：chenhuiyan
 * 时间：2020-04-30
 */
@Service
@Transactional(readOnly = true)
public class IVideosServiceImpl extends ServiceImpl<VideosMapper, Videos> implements IVideosService {

    /**
     * 查询列表
     * @param params
     * @return
     */
    public Page<Map<String, Object>> list(Map<String, Object> params) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, params);
    }

    /**
     * 上传视频
     * @param file
     * @return
     */
    @Transactional
    public Map<String, Object> uploadVideos(MultipartFile file){
        Map<String, Object> res = new HashMap<>();
        //上传
        Map<String, Object> map = UploadFileUtil.fileUploadVideoToOne(file, UploadFileUtil.VIDEOS);

        Videos videos = new Videos();

        if("200".equals(map.get("state") + "")){
            videos.setFileName(map.get("fileName") + "");
            videos.setFileOrgname(map.get("fileOrgname") + "");
            videos.setFileSize(Integer.valueOf(map.get("fileSize") + ""));
            videos.preInsert();
            this.save(videos);
            res.put("state", "200");
            res.put("message", "成功上传");
        }

        return res;
    }

    /**
     * 删除视频（物理删除）
     * @param videosId
     */
    @Transactional
    public void onDelete(String videosId){
        Videos videos = this.getById(videosId);
        boolean flag = UploadFileUtil.deleteOneFile(UploadFileUtil.VIDEOS + videos.getFileName());
        if(flag){
            this.removeById(videosId);
        }
    }
}
